<!DOCTYPE html>
<html>

<head>
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
	<meta charset="utf-8" />
	<title>设备管理</title>

	<meta name="description" content="overview &amp; stats" />
	<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0" />

	<!-- bootstrap & fontawesome -->
	<link rel="stylesheet" href="/backpage/files/assets/css/bootstrap.min.css" />
	<link rel="stylesheet" href="/backpage/files/assets/font-awesome/4.5.0/css/font-awesome.min.css" />

	<!-- page specific plugin styles -->

	<!-- text fonts -->
	<link rel="stylesheet" href="/backpage/files/assets/css/fonts.googleapis.com.css" />

	<!-- ace styles -->
	<link rel="stylesheet" href="/backpage/files/assets/css/ace.min.css" class="ace-main-stylesheet"
		id="main-ace-style" />

	<!--[if lte IE 9]>
			<link rel="stylesheet" href="/backpage/files/assets/css/ace-part2.min.css" class="ace-main-stylesheet" />
		<![endif]-->
	<link rel="stylesheet" href="/backpage/files/assets/css/ace-skins.min.css" />
	<link rel="stylesheet" href="/backpage/files/assets/css/ace-rtl.min.css" />

	<!--[if lte IE 9]>
		  <link rel="stylesheet" href="/backpage/files/assets/css/ace-ie.min.css" />
		<![endif]-->

	<!-- inline styles related to this page -->

	<!-- ace settings handler -->
	<script src="/backpage/files/assets/js/ace-extra.min.js"></script>

	<!-- HTML5shiv and Respond.js for IE8 to support HTML5 elements and media queries -->

	<!--[if lte IE 8]>
		<script src="/backpage/files/assets/js/html5shiv.min.js"></script>
		<script src="/backpage/files/assets/js/respond.min.js"></script>
		<![endif]-->

	<!--[if !IE]> -->
	<script src="/backpage/files/assets/js/jquery-2.1.4.min.js"></script>
	<!-- <![endif]-->
	<!--[if IE]>
		<script src="/backpage/files/assets/js/jquery-1.11.3.min.js"></script>
		<![endif]-->
	<!--自定义的主要css-->
	<link rel="stylesheet" href="/backpage/files/css/common.css" />


	<!--时间控件-->
	<link href="/backpage/files/js/mobiscroll/mobiscroll.frame.css" rel="stylesheet" type="text/css">
	<link href="/backpage/files/js/mobiscroll/mobiscroll.scroller.css" rel="stylesheet" type="text/css">
	<script src="/backpage/files/js/mobiscroll/mobiscroll.core.js"></script>
	<script src="/backpage/files/js/mobiscroll/mobiscroll.frame.js"></script>
	<script src="/backpage/files/js/mobiscroll/mobiscroll.scroller.js"></script>
	<script src="/backpage/files/js/mobiscroll/mobiscroll.util.datetime.js"></script>
	<script src="/backpage/files/js/mobiscroll/mobiscroll.datetimebase.js"></script>
	<script src="/backpage/files/js/mobiscroll/mobiscroll.datetime.js"></script>
	<script src="/backpage/files/js/mobiscroll/mobiscroll.i18n.zh.js"></script>

</head>

<body class="no-skin">

	<div class="main-container ace-save-state" id="main-container">

		<div class="main-content" style="margin-left: 0px;">
			<div class="main-content-inner">
				<div class="page-content">


					<!-- /.page-header
					<div class="page-header">
						<h1>用户管理</h1>
					</div>
					-->

					<div class="row">
						<div class="col-xs-12">

							<div>
								<div id="dynamic-table_wrapper" class="dataTables_wrapper form-inline no-footer">
									<div class="row">
										<div class="col-xs-12">
											<button class="btn btn-success btn-sm"
												onclick="javascirpt:window.location.reload(true);">刷新</button>
											<button class="btn btn-primary btn-sm" onclick="addUser()">添加</button>
											<!--<button class="btn btn-danger btn-sm" onclick="delByIds()">删除</button>-->

											<input type="file" style="display: none" id="icon"
												accept="application/vnd.ms-excel" onchange="import_excel()" />
											<button class="btn btn-success btn-sm"
												onclick="$('#icon').val('');$('#icon').click();">导入excel</button>


											<button class="btn btn-primary btn-sm" onclick="window.location.href='/device/export_excel'">导出excel</button>
											<input type="search" class="form-control input-sm" placeholder="设备名"
												id="searchText">
											<button class="btn btn-success btn-sm" onclick="list(true)">搜索</button>
										</div>
									</div>
									<table id="simple-table"
										class="table table-striped table-bordered table-hover dataTable no-footer"
										role="grid" aria-describedby="dynamic-table_info">
										<thead>
											<tr role="row">
												<!--<th class="center sorting_disabled" style="width:3%"><label
													class="pos-rel"> <input type="checkbox" class="ace">
														<span class="lbl"></span> </label>
												</th>-->
												<th>区县</th>
												<th>站点名称</th>
												<th>设备编号</th>
												<th>设备位置</th>
												<th>设备状态</th>
												<th>告警状态</th>
												<th>断电时间</th>
												<th>信号</th>
												<th>操作</th>
											</tr>
										</thead>

										<tbody id="mainTbody">

										</tbody>
									</table>
									<div class="row">
										<div class="col-xs-12">
											<ul id="pageLimit" style="margin: 0;display: inline;cursor: pointer ; ">
											</ul>
										</div>
									</div>
								</div>
							</div>

						</div>
						<!-- /.col -->
					</div>
					<!-- /.row -->
				</div>
				<!-- /.page-content -->
			</div>
		</div>
		<!-- /.main-content -->
	</div>
	<!-- /.main-container -->


	<!--弹出框 -->
	<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
		<div class="modal-dialog">
			<div class="modal-content">
				<div class="modal-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
					<h4 class="modal-title" id="myModalLabel">用户信息</h4>
				</div>
				<div class="modal-body col-sm-12">
					<div class="col-sm-12">
						<label class="col-sm-3 text-right">设备名称</label>
						<input class="col-sm-8" id="name">
						<label class="col-sm-1 redText">*</label>
					</div>
					<div class="col-sm-12">
						<label class="col-sm-3 text-right">设备编号</label>
						<input class="col-sm-8" id="id">
						<label class="col-sm-1 redText">*</label>
					</div>
					<div class="col-sm-12">
						<label class="col-sm-3 text-right">区县</label>
						<input class="col-sm-8" id="area">
					</div>
					<div class="col-sm-12">
						<label class="col-sm-3 text-right">位置</label>
						<input class="col-sm-8" id="position">
					</div>
				</div>
				<div class="modal-footer">
					<button type="button" class="btn btn-default btn-sm" data-dismiss="modal">关闭</button>
					<button type="button" class="btn btn-primary btn-sm" onclick="save()">提交</button>
				</div>
			</div>
			<!-- /.modal-content -->
		</div>
		<!-- /.modal-dialog -->
	</div>
	<!-- /.modal -->


	<!--弹出框 定时-->
	<div class="mymodal fade" id="timeModal">
		<div class="mymodal-dialog">
			<div class="mymodal-content">
				<div class="mymodal-header">
					<button type="button" class="close"
						onclick="$(this).parent().parent().parent().parent().hide();">×</button>
					<h4 class="mymodal-title" id="timeModalLabel">用户信息</h4>
				</div>
				<div class="mymodal-body col-sm-12">
					<div class="col-sm-12 mb5 ">
						<button type="button" class="btn btn-primary btn-xs" id="addTimeButton">添加</button>
					</div>
					<div class="col-sm-12 " id="timeList">
						无数据
					</div>
				</div>
				<div class="mymodal-footer">
					<button type="button" class="btn btn-default btn-sm" data-dismiss="mymodal"
						onclick="$(this).parent().parent().parent().parent().hide();">关闭</button>
					<!--<button type="button" class="btn btn-primary btn-sm"  onclick="save()">提交</button>-->
				</div>
			</div>
			<!-- /.modal-content -->
		</div>
		<!-- /.modal-dialog -->
	</div>
	<!-- /.modal -->


	<!-- basic scripts -->

	<script src="/backpage/files/assets/js/bootstrap.min.js"></script>

	<!-- page specific plugin scripts -->

	<!--[if lte IE 8]>
		  <script src="/backpage/files/assets/js/excanvas.min.js"></script>
		<![endif]-->
	<script src="/backpage/files/assets/js/jquery-ui.custom.min.js"></script>
	<script src="/backpage/files/assets/js/jquery.ui.touch-punch.min.js"></script>
	<script src="/backpage/files/assets/js/jquery.easypiechart.min.js"></script>
	<script src="/backpage/files/assets/js/jquery.sparkline.index.min.js"></script>
	<script src="/backpage/files/assets/js/jquery.flot.min.js"></script>
	<script src="/backpage/files/assets/js/jquery.flot.pie.min.js"></script>
	<script src="/backpage/files/assets/js/jquery.flot.resize.min.js"></script>

	<!-- ace scripts -->
	<script src="/backpage/files/assets/js/ace-elements.min.js"></script>
	<script src="/backpage/files/assets/js/ace.min.js"></script>

	<script src="/backpage/files/js/bootstrap-paginator.js"></script>
	<script src="/backpage/files/js/md5.js"></script>
	<script src="/backpage/files/js/common.js"></script>

	<script type="text/javascript">
		var active_class = 'active';//复选框样式
		var currentPage = 1;//当前页
		var pageSize = 20;//显示多少页
		var countPage = 0;//一共多少页
		var objects = [];//当前页面数据
		var object = {};//某一组数据
		var clients = [];//链接到MQTT的当前客户端

		/*复选框*/
		$('#simple-table > thead > tr > th input[type=checkbox]').eq(0).on('click', function () {
			var th_checked = this.checked;//checkbox inside "TH" table header
			$(this).closest('table').find('tbody > tr').each(
				function () {
					var row = this;
					if (th_checked)
						$(row).find(
							'input[type=checkbox]').eq(0).prop(
								'checked', true);
					else
						$(row).find(
							'input[type=checkbox]').eq(0).prop(
								'checked', false);
				});
		});


		/**
		 * 打开设备 
		 */
		function openSheBei(id) {
			disabledButton(id);
			var msgJson = {};
			msgJson.id = id;
			msgJson.command = 1;
			msgJson.type = "control";
			sendMqtt(JSON.stringify(msgJson), id);
		}

		function closeSheBei(id) {
			disabledButton(id);
			var msgJson = {};
			msgJson.id = id;
			msgJson.command = 0;
			msgJson.type = "control";
			sendMqtt(JSON.stringify(msgJson), id);
		}

		function disabledButton(id) {
			$("#openBtn"+id).attr("disabled", "disabled");
			$("#closeBtn"+id).attr("disabled", "disabled");
			setTimeout(function () {
				$("#openBtn"+id).removeAttr("disabled", "disabled");
				$("#closeBtn"+id).removeAttr("disabled", "disabled");
			}, 5000);
			setTimeout(function () {
				//getDeviceStatus(id);
			}, 3000);
		}

		/*查询，分页*/
		function list(ifSearch) {

			if (ifSearch) {
				currentPage = 1;
			}

			var data = {
				currentPage: currentPage,
				pageSize: pageSize,
				searchText: $("#searchText").val()
			};

			var dataSubmit = {};
			dataSubmit.data = JSON.stringify(data);

			$.ajax({
				url: '/device/list',
				type: 'POST',
				async: true,
				data: dataSubmit,
				timeout: 5000,
				dataType: 'json',
				success: function (data, textStatus, jqXHR) {

					var count = data.count;
					var data = data.data;
					objects = data;
					var html = "";

					console.log(data);
					clients=[];
					for (var i = 0; i < data.length; i++) {
						var obj = data[i];
						var statusName = "";
						var statusOperatName = "";
						if (obj.status == 1) {
							statusName = "启用";
							statusOperatName = "禁用";
						}
						if (obj.status == 0) {
							statusName = "禁用";
							statusOperatName = "启用";
						}
						

						html += '<tr>' +
							'	<!--<td class="center">';
						html += '<label class="pos-rel"><input type="checkbox" class="ace" value="' + obj.id + '"/> <span class="lbl"></span></label>';
						html += '	</td>-->' +
							'	<td>' + obj.area + '</td>' +
							'	<td>' + obj.name + '</td>' +
							'	<td>' + obj.id + '</td>' +
							'	<td>' + obj.position + '</td>' +
							'	<td id="status' + obj.id + '">未连接</td>' +
							'	<td id="alarm' + obj.id + '" class="greenColor">正常</td>' +
							'	<td>0</td>' +
							'	<td><span class="glyphicon glyphicon-signal'+(i>3?"":i)+'"></span></td>' +
							'	<td>' +
							'		<button id="openBtn'+obj.id+'" class="btn btn-success btn-xs" onclick="openSheBei(\'' + obj.id + '\')">开启</button>' +
							'		<button id="closeBtn'+obj.id+'" class="btn btn-danger btn-xs" onclick="closeSheBei(\'' + obj.id + '\')">关闭</button>' +
							'		<button class="btn btn-success btn-xs" onclick="update(\'' + obj.id + '\')">修改</button>' +

							'		<button class="btn btn-pink btn-xs" onclick="addTime(\'' + obj.id + '\',\'' + obj.name + '\',\'普通定时\',\'1\')">普通定时</button>' +
							'		<button class="btn btn-primary btn-xs" onclick="addTime(\'' + obj.id + '\',\'' + obj.name + '\',\'重复定时\',\'2\')">重复定时</button>' +
							'		<button class="btn btn-success btn-xs" onclick="addTime(\'' + obj.id + '\',\'' + obj.name + '\',\'周定时\',\'3\')">周定时</button>' +
							'		<button class="btn btn-danger btn-xs" onclick="delDevice(\'' + obj.id + '\')">删除</button>' +
							'  </td>' +
							'</tr>';
					}
					
					$("#mainTbody").html(html);

					countPage = Math.ceil(count / pageSize);
					$('#pageLimit').bootstrapPaginator({
						currentPage: currentPage,
						totalPages: countPage,
						size: "normal",
						bootstrapMajorVersion: 3,
						alignment: "left",
						shouldShowPage: true,
						numberOfPages: pageSize,//显示多少页

						onPageClicked: function (event, originalEvent, type, page) {//给每个页面绑定一个事件，其实就是ajax请求，其中page变量为当前点击的页上的数字。
							if (page < 1) {
								currentPage = 1;
							} else if (page > countPage) {
								currentPage = countPage;
							} else {
								currentPage = page;
							}
							list();
						}
					});

					$('#pageLimit').append("<li style='cursor: text; '>&nbsp;&nbsp;共" + countPage + "页，跳转到&nbsp;<input id='pageNum' style='width:40px;'/>&nbsp;页</li>&nbsp;<button class='btn btn-success btn-xs' onclick='javascript:goPage()'>确定</button>");

				},
				error: function (xhr, textStatus) {
					console.log('错误');
					console.log(xhr);
					console.log(textStatus);
				}
			})
		}
		//跳页
		function goPage() {
			var pageNum = $("#pageNum").val()
			if(pageNum=="0"||pageNum==""){
				return;
			}
			if (Number(pageNum) > countPage) {
				return;
			} else {
				currentPage = pageNum;
				list();
			}
		}

		


		function getClientByDeviceId(deviceId){
			for(var i=0;i<clients.length;i++){
				var obj=clients[i];
				if(obj.deviceId==deviceId){
					return obj.client;
				}
			}
		}
		//设备状态包，设备即时返回一个心跳
		function getDeviceStatus(deviceId) {
			var msgJson = {};
			msgJson.id = deviceId;
			msgJson.command = 2;
			msgJson.type = "control";
			sendMqtt(JSON.stringify(msgJson), deviceId);
		}


		/*添加，修改**/
		function getObjById(id) {
			if (objects.length > 0) {
				for (var i = 0; i < objects.length; i++) {
					var obj = objects[i];
					if (id == obj.id) {
						return obj;
					}
				}
			}
		}
		//添加弹出
		function addUser() {
			$("#myModal").modal('show');
			$("#myModalLabel").html("添加用户");

			object.id = "";
			$("#id").val("");
			$("#id").removeAttr("disabled");
			$("#name").val("");
			$("#area").val("");
			$("#position").val("");
		}
		//修改弹出
		function update(id) {
			var obj = getObjById(id);

			$("#myModal").modal('show');
			$("#myModalLabel").html("修改设备");

			object.id = obj.id;
			$("#id").attr("disabled", "disabled");
			$("#id").val(obj.id);
			$("#name").val(obj.name);
			$("#area").val(obj.area);
			$("#position").val(obj.position);
		}

		//添加，修改
		function save() {
			var name = $("#name");

			//去空格
			$("#name").val($("#name").val().trim());


			//验证
			if (!name || name.val() == "") {
				alert("设备名称不能为空！");
				name.focus();
				return;
			}
			if (object.id == "") {//添加才验证
				if ($("#id").val() == "") {
					alert("设备编号不能为空！");
					$("#id").focus();
					return;
				}
				if (checkDeviceId($("#id").val())) {
					alert("设备id已存在！");
					$('#id').focus();
					return;
				}
			}

			var data = {
				id: $("#id").val(),//设备编号
				name: name.val().trim(),//设备名称
				area: $("#area").val(),
				position: $("#position").val()
			};

			var dataSubmit = {};
			dataSubmit.data = JSON.stringify(data);
			if (object.id != "") {
				var sql = "update device set name='" + data.name + "',area='" + data.area + "',position='" + data.position + "' where id='" + data.id + "'";
			}
			if (object.id == "") {
				var sql = "insert into device(id,name,userid,area,position) values('" + data.id + "','" + data.name + "','01','"+data.area+"','"+data.position+"')";
			}

			executeSql(sql, function (data) {
				if (data.rtn == 1) {
					alert("操作成功");
					$("#myModal").modal('hide');
					list();
				}
				if (data.rtn == 0) {
					alert("操作失败");
				}
			});

		}

		//导出
		function import_excel() {
			var formData = new FormData();
			formData.set("file", $("#icon")[0].files[0]);

			$.ajax({
				url: '/device/import_excel',
				type: 'POST',
				async: false,
				processData: false,//这个很有必要，不然不行
				contentType: false,
				data: formData,
				success: function (data, textStatus, jqXHR) {
					console.log(data);
					if (data.rtn > 0) {
						alert("操作成功！");
						list();
					} else {
						alert("操作失败，设备编号不能重复！")
						list();
					}
				},
				error: function (xhr, textStatus) {
					console.log('错误');
					console.log(xhr);
					console.log(textStatus);
				}
			});

		}


		function checkDeviceId(id) {
			var sql = "select * from device where id='" + id + "'";
			var ifRepeat = false;
			listSql(sql, function (data) {
				if (data.length > 0) {
					ifRepeat = true;
				}
			}, false);
			return ifRepeat;
		}


		function delDevice(id) {
			if (confirm("是否删除")) {
				var data = {};
				data.id = id;
				$.ajax({
					url: '/common/delDevice',
					type: "post",
					async: true,
					data: data,
					success: function (data) {
						console.log("result:" + data);
						if (data == 1) {
							alert("成功！");
							list();
						}
						if (data == 0) {
							alert("失败！");
							//listSheBei();
						}
						$("div[onclick*='delJob']").removeAttr("disabled");
					},
					error: function (error) {
						alert(JSON.stringify(error));
					}
				})
			}
		}




		/*删除*/
		function delByIds(id) {
			var ids = [];
			if (id) {
				ids.push(id);
			} else {
				$('#mainTbody > tr > td input[type=checkbox]').each(function () {
					if ($(this).prop("checked")) {
						ids.push($(this).val());
					}
				})
			}

			if (ids.length > 0) {
				if (confirm("确定删除！")) {
					var sql = "delete from device where id in (";
					for (var i = 0; i < ids.length; i++) {
						var id = ids[i];
						if (i == (ids.length - 1)) {
							sql += "\'" + id + "\')";
						} else {
							sql += "\'" + id + "\',";
						}
					}

					executeSql(sql, function (data) {
						if (data.rtn == 1) {
							alert("操作成功");
							$("#myModal").modal('hide');
							list();
						}
						if (data.rtn == 0) {
							alert("操作失败");
						}
					});

				}
			} else {
				alert("未选择任何值！");
			}
		}

		/*定时********************************************************************************/


		var myJob = {};

		//id:设备id
		function initTime(id, type) {

			var onOffHtml = '<div class="onOffDiv">	' +
				'<label style="text-align: center;"><input type="radio" name="onOff" checked="checked" value="1"/><br>开启</label>&nbsp;&nbsp;' +
				'<label style="text-align: center;"><input type="radio" name="onOff" value="0"/><br>关闭</label>' +
				'</div>';
			var weekHtml = '<div class="weekDiv">' +
				'&nbsp;<label><input type="radio" name="weekInput" checked="checked" value="2"/><br>周一</label>&nbsp;&nbsp;' +
				'<label><input type="radio" name="weekInput"  value="3"/><br>周二</label>&nbsp;&nbsp;' +
				'<label><input type="radio" name="weekInput"  value="4"/><br>周三</label>&nbsp;&nbsp;' +
				'<label><input type="radio" name="weekInput"  value="5"/><br>周四</label>&nbsp;&nbsp;' +
				'<label><input type="radio" name="weekInput"  value="6"/><br>周五</label>&nbsp;&nbsp;' +
				'<label><input type="radio" name="weekInput"  value="7"/><br>周六</label>&nbsp;&nbsp;' +
				'<label><input type="radio" name="weekInput"  value="1"/><br>周日</label>&nbsp;' +
				'</div>';
			if (type == 1) {

				$('#addTimeButton').mobiscroll().datetime({//普通定时
					lang: 'zh',       // https://docs.mobiscroll.com/2-14-0/jquery/datetime#localization-lang
					onShow: function (html, valueText, inst) {
						$(".dwbc").append(onOffHtml);
						myJob.week = 0;
					},
					onClose: function (valueText, btn, inst) {
						console.log($("input[name='onOff']:checked").val());
						myJob.control = $("input[name='onOff']:checked").val();//控制，0关闭设备，1打开设备
					},
					onSelect: function (valueText, inst) {
						myJob.time = timeToNumber(valueText);//任务执行时间
						myJob.type = 1;//任务类型，1执行一次，2执行无数次
						console.log(time_yyyyMMdd_hhmm(valueText));
						addJob(id, type)
					}
				});
			}
			if (type == 2) {

				$('#addTimeButton').mobiscroll().time({//重复定时
					lang: 'zh',       // https://docs.mobiscroll.com/2-14-0/jquery/datetime#localization-lang
					onShow: function (html, valueText, inst) {
						$(".dwbc").append(onOffHtml);
						myJob.week = 0;
					},
					onClose: function (valueText, btn, inst) {
						console.log($("input[name='onOff']:checked").val());
						myJob.control = $("input[name='onOff']:checked").val();//控制，0关闭设备，1打开设备
					},
					onSelect: function (valueText, inst) {
						valueText = getYear() + "-" + getMonth() + "-" + getDate() + " " + valueText;
						myJob.time = timeToNumber(valueText);//任务执行时间
						myJob.type = 2;//任务类型，1执行一次，2执行无数次
						console.log(time_yyyyMMdd_hhmm(valueText));
						addJob(id, type)
					}
				});
			}
			if (type == 3) {

				$('#addTimeButton').mobiscroll().time({//周定时
					lang: 'zh',       // https://docs.mobiscroll.com/2-14-0/jquery/datetime#localization-lang
					onShow: function (html, valueText, inst) {
						myJob.week = 0;
						$(".dwcc").html(weekHtml + $(".dwcc").html());
						$(".dwbc").append(onOffHtml);
					},
					onClose: function (valueText, btn, inst) {
						console.log($("input[name='onOff']:checked").val());
						myJob.control = $("input[name='onOff']:checked").val();//控制，0关闭设备，1打开设备
						myJob.week = $("input[name='weekInput']:checked").val();//每周星期几执行
					},
					onSelect: function (valueText, inst) {
						valueText = getYear() + "-" + getMonth() + "-" + getDate() + " " + valueText;
						myJob.time = timeToNumber(valueText);//任务执行时间
						myJob.type = 2;//任务类型，1执行一次，2执行无数次
						console.log(time_yyyyMMdd_hhmm(valueText));
						addJob(id, type)
					}
				});
			}

			/*
			var sql="select * from device where id='"+deviceId+"'";
			listSql(sql,function(data){
				if(data.length>0){
					$($("#header>div")[1]).html(data[0].name);
				}
			});
			
			list();
			*/
		}
		//id:设备id
		function addJob(id, type) {
			myJob.id = uuid();
			myJob.deviceId = id;
			var data = {};
			data.bean = JSON.stringify(myJob);
			console.log(JSON.stringify(myJob));
			$.ajax({
				url: '/common/addJob',
				type: "post",
				async: true,
				data: data,
				success: function (data) {
					console.log("result:" + data);
					if (data == 1) {
						alert("成功！");
						listTime(id, type);
					}
					if (data == 0) {
						alert("失败！");
						//list(id,type);
					}
				},
				error: function (error) {
					alert(JSON.stringify(error));
				}
			})
		}
		//删除任务
		function delJob(id, deviceId, type) {

			if (confirm("是否删除")) {
				$("button[onclick*='delJob']").attr("disabled", "disabled");
				var data = {};
				data.id = id;
				$.ajax({
					url: '/common/delJob',
					type: "post",
					async: true,
					data: data,
					success: function (data) {
						console.log("result:" + data);
						if (data == 1) {
							alert("成功！");
							listTime(deviceId, type);
						}
						if (data == 0) {
							alert("失败！");
							//list();
						}
						$("div[onclick*='delJob']").removeAttr("disabled");
					},
					error: function (error) {
						alert(JSON.stringify(error));
					}
				})
			}

		}

		/*设置定时弹出      id:设备id name:设备名 typeName:定时类型名    */
		function addTime(id, name, typeName, type) {
			//$("#timeModal").modal('show');
			$("#timeModal").show();
			$("#timeModal").css("opacity", "1");
			$("#timeModalLabel").html(name + "的" + typeName);


			listTime(id, type);

			initTime(id, type);

		}


		function listTime(id, type) {
			var sql = "";
			if (type == 1 || type == 2) {
				sql = "SELECT * FROM myjob where deviceId='" + id + "' and \"type\"=" + type + " and week=0";
			}
			if (type == 3) {
				sql = "SELECT * FROM myjob where deviceId='" + id + "' and \"type\"=2 and week!=0";
			}
			var html = "";
			var typeP = type;
			listSql(sql, function (data) {
				console.log(data);
				for (var i = 0; i < data.length; i++) {
					var obj = data[i];
					var type = "";
					var time = "";
					var control = "";

					if (obj.control == 0) {
						control = "关闭";
					}
					if (obj.control == 1) {
						control = "开启";
					}

					if (obj.type == 1) {
						type = "仅此一次 ";
						time = time_yyyyMMdd_hhmm(obj.time);
					}
					if (obj.type == 2) {
						if (obj.week == 0) {
							type = "每天 ";
							var t = new Date();
							t.setTime(obj.time);
							var m = t.getMinutes();
							if (m < 10) {
								m = "0" + m;
							}
							time = t.getHours() + ":" + m;
						}
						if (obj.week != 0) {
							var t = new Date();
							t.setTime(obj.time);
							var m = t.getMinutes();
							if (m < 10) {
								m = "0" + m;
							}
							type = "每周";
							time = convertWeek(obj.week) + " " + t.getHours() + ":" + m;
						}

					}
					html += '<div class="col-sm-12 mb5 " >' +
						'	<div class="col-sm-2">' +
						'		' + type + '</div><div class="col-sm-4">' +
						'		' + time + '</div><div class="col-sm-2">' +
						'		' + control + '</div><div class="col-sm-2">' +
						'		<button class="btn btn-danger btn-xs" onclick="delJob(\'' + obj.id + '\',\'' + id + '\',\'' + typeP + '\')">删除</button></div>' +
						'	</div>' +
						'</div>';

				}
				$("#timeList").html(html);
			});
		}



		function convertWeek(week) {
			var result = "";
			week == 2 ? result = "周一" : result = result;
			week == 3 ? result = "周二" : result = result;
			week == 4 ? result = "周三" : result = result;
			week == 5 ? result = "周四" : result = result;
			week == 6 ? result = "周五" : result = result;
			week == 7 ? result = "周六" : result = result;
			week == 1 ? result = "周日" : result = result;
			return result;
		}





		function init() {
			//initTime();
		}


		/*初始*/
		list();
		init();
		//initRoleSelect();
		//initDeptSelect();
	</script>
</body>

</html>